Imports KanBan.Pgd.Infrastructure.EventLog
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views

Public Class ShowEventLog

    Private Const DefaulGridViewRowHeight As Integer = 20

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim logs As IList(Of Log) = EventLogger.GetAll()

        Me.AddLogs(logs)

    End Sub

#Region "IShowEventLogView"

    ''' <summary>
    ''' Agrega Logs a la grilla de Logs
    ''' </summary>
    ''' <param name="logs"></param>
    ''' <remarks></remarks>
    Public Sub AddLogs(ByVal logs As IEnumerable(Of Log))

        For Each l As Log In logs
            AddLog(l)
        Next

    End Sub

    ''' <summary>
    ''' Muestra los detalles de un Log
    ''' </summary>
    ''' <param name="log"></param>
    ''' <remarks></remarks>
    Public Sub ShowLogEntryDetails(ByVal log As Log)

        txtDetails.Text = log.Message

    End Sub

#End Region

#Region "Event Handling"

    Private Sub grvLogs_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

        If e.RowIndex >= 0 Then

            Dim logId As Integer = CInt(grvLogs.Rows(e.RowIndex).Cells(GridColumns.Id).Value)

            Me.ShowLogEntryDetails(logId)

        End If

    End Sub

    Public Sub ShowLogEntryDetails(ByVal logId As Integer)

        Dim eventLog As Log = EventLogger.GetById(logId)

        Me.ShowLogEntryDetails(eventLog)

    End Sub

#End Region

#Region "Private"

    Private Sub AddLog(ByVal log As Log)

        Dim row As New DataGridViewRow()
        row.CreateCells(grvLogs)
        row.Height = DefaulGridViewRowHeight

        row.Cells(GridColumns.Id).Value = log.Id
        row.Cells(GridColumns.Level).Value = log.Level
        row.Cells(GridColumns.LevelImage).Value = GetLevelImage(log.Level)
        row.Cells(GridColumns.DateTime).Value = log.EventDate
        row.Cells(GridColumns.Source).Value = log.Source

        grvLogs.Rows.Add(row)

    End Sub

    Private Function GetLevelImage(ByVal level As EventLevel) As Bitmap

        Dim resourcesType As Type = GetType(ManageSearchRequests)

        Select Case (level)
            Case EventLevel.Info
                Return New Bitmap(resourcesType, "Info_log.png")
            Case EventLevel.Error
                Return New Bitmap(resourcesType, "Error_log.png")
            Case EventLevel.Warning
                Return New Bitmap(resourcesType, "Warning_log")
            Case Else
                Return New Bitmap(resourcesType, "Info_log.png")

        End Select

    End Function

    Private Enum GridColumns
        Id = 0
        LevelImage = 1
        Level = 2
        DateTime = 3
        Source = 4
    End Enum

#End Region

End Class